export function getBeforeTime(type, val){
	const date = new Date();
	if(type === 4 || type === 1){
		date.setHours(Number(date.getHours()) - Number(val));
	}else if(type === 5 || type === 2){
		date.setMinutes(Number(date.getMinutes()) - Number(val));
	}else if(type === 6 || type === 3){
		date.setSeconds(Number(date.getSeconds()) - Number(val));
	}
	return date;
}
export function getLaterTime(type, val){
	const date = new Date();
	if(type === 4 || type === 1){
		date.setHours(Number(date.getHours()) + Number(val));
	}else if(type === 5 || type === 2){
		date.setMinutes(Number(date.getMinutes()) + Number(val));
	}else if(type === 6 || type === 3){
		date.setSeconds(Number(date.getSeconds()) + Number(val));
	}
	return date;
}
export function getScriptFunc(str){
	let func = null;
	try{
		func = eval(str);
		return func;
	}catch(error){
		console.log(error);
		return false;
	}
}
export function toDate(v, format){
	format = format || 'yyyy-MM-dd HH:mm';
	if(!v) return '';
	let d = v;
	if(typeof v === 'string'){
		if(v.indexOf('/Date(') > -1){
			d = new Date(parseInt(v.replace('/Date(', '').replace(')/', ''), 10));
		}else d = new Date(Date.parse(v.replace(/-/g, '/').replace('T', ' ').split('.')[0]));
	}else{
		d = new Date(v);
	}
	let o = {
		'M+':d.getMonth() + 1,
		'd+':d.getDate(),
		'h+':d.getHours(),
		'H+':d.getHours(),
		'm+':d.getMinutes(),
		's+':d.getSeconds(),
		'q+':Math.floor((d.getMonth() + 3) / 3),
		S:d.getMilliseconds(),
	};
	if(/(y+)/.test(format)){
		format = format.replace(RegExp.$1, (d.getFullYear() + '').substr(4 - RegExp.$1.length));
	}
	for(let k in o){
		if(new RegExp('(' + k + ')').test(format)){
			format = format.replace(
				RegExp.$1,
				RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length),
			);
		}
	}
	return format;
}
export function cloneDeep(obj, hash = new WeakMap()){
	// 如果 obj 是 null 或 undefined，则直接返回
	if(obj === null || obj === undefined) return obj;
	// 检查是否为基本类型（不需要深拷贝）
	if(typeof obj !== 'object') return obj;
	// 处理循环引用
	if(hash.has(obj)) return hash.get(obj);
	// 处理日期和正则
	if(obj instanceof Date) return new Date(obj);
	if(obj instanceof RegExp) return new RegExp(obj);
	// 创建拷贝的对象或数组
	const copy = Array.isArray(obj) ? [] : {};
	// 在 hash 中存储对象的引用
	hash.set(obj, copy);
	// 遍历对象的属性并递归进行深拷贝
	for(let key in obj){
		if(Object.prototype.hasOwnProperty.call(obj, key)){
			copy[key] = cloneDeep(obj[key], hash);
		}
	}
	return copy;
}